package com.atguigu.chapter11;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/7/24 10:03
 */
public class Flink06_Sql_File {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
    
        
        
        StreamTableEnvironment tenv = StreamTableEnvironment.create(env);
        
        // 1. 建一张动态表与文件进行关联(source)
        tenv.executeSql("create table sensor(" +
                            "   id string, " +
                            "   ts  bigint," +
                            "   vc int" +
                            ")with(" +
                            "   'connector' = 'filesystem', " +
                            "   'path' = 'input/sensor.txt'," +
                            "   'format' = 'csv'" +
                            ")");
    
        Table table = tenv.sqlQuery("select id, vc from sensor");
        
        
        // 2. 建一张动态表与文件进行关联(sink)
        tenv.executeSql("create table s1(" +
                            "   id string, " +
                            "   vc int" +
                            ")with(" +
                            "   'connector' = 'filesystem', " +
                            "   'path' = 'input/a'," +
                            "   'format' = 'csv'" +
                            ")");
        
        // 3. 把table数据写入到sink表
        table.executeInsert("s1");
    
    }
}
